/*   
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.fanfq.smplayer.media;

import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.fanfq.smplayer.SMPlayerApplication;
import com.tsai.english.model.MusicItem;

/**
 * Retrieves and organizes media to play. Before being used, you must call {@link #prepare()},
 * which will retrieve all of the music on the user's device (by performing a query on a content
 * resolver). After that, it's ready to retrieve a random song, with its title and URI, upon
 * request.
 */
public class MusicRetriever {
    final String TAG = "MusicRetriever";

    ContentResolver mContentResolver;
    private SMPlayerApplication smpp;

    // the items (songs) we have queried
//    List<Item> mItems = new ArrayList<Item>();

    Random mRandom = new Random();

    public MusicRetriever(ContentResolver cr) {
        mContentResolver = cr;
    }

    /**
     * Loads music data. This method may take long, so be sure to call it asynchronously without
     * blocking the main thread.
     */
    public void prepare(SMPlayerApplication smpp_) {
    	smpp = smpp_;
    }

    public ContentResolver getContentResolver() {
        return mContentResolver;
    }
    
    public MusicItem getNextItem(int mode){
    	
    	List<MusicItem> playlist = smpp.getPlayerList();
    	MusicItem currentPlaying = smpp.getCurrentPlaying();
    	int playingIndex = smpp.getCurrentPlayingIndex();
    	
    	System.out.println("playlist.size()"+playlist.size());
    	for(MusicItem l:playlist){
    		System.out.println(l.getMp3_url()+","+l.getMp3_sdcard_path());
    		System.out.println(l.getTxt_url()+","+l.getTxt_sdcard_path());
    	}
    	
    	if(playlist.size() <= 0){
			return null;
		}
    	if(mode == 0){
	    	switch (smpp.getPlayerMode()) {
			case 0://single loop
				
				break;
			case 1://list loop
				if(playingIndex < playlist.size() - 1){
					playingIndex ++;
				}else{
					playingIndex = 0;
				}
				break;
			case 2://stuff
				playingIndex = mRandom.nextInt(playlist.size());
				break;
			}
    	}else if(mode ==1){
    		if(playingIndex > 0){
				playingIndex --;
			}else{
				playingIndex = playlist.size() - 1;
			}
    	}else if(mode ==2){
    		if(playingIndex < playlist.size() - 1){
				playingIndex ++;
			}else{
				playingIndex = 0;
			}
    	}
    	
    	currentPlaying = playlist.get(playingIndex);
    	smpp.setCurrentPlaying(currentPlaying);
    	smpp.setCurrentPlayingIndex(playingIndex);
    	return currentPlaying;
    	
    }

    /** Returns a random Item. If there are no items available, returns null. */
//    public Item getRandomItem() {
//        if (mItems.size() <= 0) return null;
//        return mItems.get(mRandom.nextInt(mItems.size()));
//    }

//    public static class Item {
//        long id;
//        String artist;
//        String title;
//        String album;
//        long duration;
//
//        public Item(long id, String artist, String title, String album, long duration) {
//            this.id = id;
//            this.artist = artist;
//            this.title = title;
//            this.album = album;
//            this.duration = duration;
//        }
//
//        public long getId() {
//            return id;
//        }
//
//        public String getArtist() {
//            return artist;
//        }
//
//        public String getTitle() {
//            return title;
//        }
//
//        public String getAlbum() {
//            return album;
//        }
//
//        public long getDuration() {
//            return duration;
//        }
//
//        public Uri getURI() {
//            return ContentUris.withAppendedId(
//                    android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id);
//        }
//    }
}
